package org.noote.spidy.core;

import java.util.ArrayList;
import java.util.HashMap;

import org.noote.libs.bluetooth.DeviceList_Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;

import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class Setting_Activity extends Activity {
	
	private static final int GETTING_BT_DEVICE = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		
		ListView listSettings = (ListView)findViewById(R.id.listSettings);
		listSettings.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				goSettingsPage(position);
			}
		});
		
		View viewSelectedDevice = findViewById(R.id.viewSelectedDevice);
		viewSelectedDevice.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
		           Intent intent = new Intent(getApplicationContext(), DeviceList_Activity.class);
		           startActivityForResult(intent, GETTING_BT_DEVICE);
			}
		});
		
		loadSettings();
		
		createList();
		goSettingsPage(0);
	}
	
	private void loadSettings() {
		SharedPreferences settings = getSharedPreferences(SpidyCore_Activity.PREFS_NAME, 0);
		
		// Bluetooth
        String sAddressDevice = settings.getString(SpidyCore_Activity.PREFS_NAME_BTADDRESS, "(Click here to select a device)");
		TextView textBTSelectedDevice = (TextView)findViewById(R.id.textBTSelectedDevice);
		textBTSelectedDevice.setText(sAddressDevice);
		
		// Server Port
        String sServerPort = settings.getString(SpidyCore_Activity.PREFS_NAME_SERVER_PORT, "8086");
		TextView textIPServerPort = (TextView)findViewById(R.id.textIPServerPort);
		textIPServerPort.setText(sServerPort);

		// Camera Broadcast port
        String sCameraPort = settings.getString(SpidyCore_Activity.PREFS_NAME_CAMERA_PREVIEW_PORT, "8088");
		TextView textCameraBroadcastPort = (TextView)findViewById(R.id.textCameraBroadcastPort);
		textCameraBroadcastPort.setText(sCameraPort);

		try {
			String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
	        int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
	        
	        TextView textVersion = (TextView)findViewById(R.id.textVersion);
	        textVersion.setText(Html.fromHtml("<i>version "+versionName+" build "+Integer.toString(versionCode)+"</i>"));
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	protected void goSettingsPage(int iPage) {
		ViewFlipper _flipper = (ViewFlipper)findViewById(R.id.flipperSettings);
		_flipper.setInAnimation(this, R.anim.view_transition_in);
		_flipper.setOutAnimation(this, R.anim.view_transition_out);
		_flipper.setDisplayedChild(iPage);
	}
	
	void createList()
	{
		ListView listSettings = (ListView)findViewById(R.id.listSettings);
		
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        
        map = new HashMap<String, String>();
        map.put("title", getString(R.string.text_title_version));
        map.put("subtitle", getString(R.string.text_subtitle_version));
        listItem.add(map);
        
        map = new HashMap<String, String>();
        map.put("title", getString(R.string.text_title_network));
        map.put("subtitle", getString(R.string.text_subtitle_network));
        listItem.add(map);
        
		SimpleAdapter adapter = new SimpleAdapter (this.getBaseContext(), listItem, android.R.layout.simple_list_item_2,
    			new String[] {"title", "subtitle"}, new int[] {android.R.id.text1, android.R.id.text2});
		
		listSettings.setAdapter(adapter);
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data)
	{
		switch(requestCode)
		{
		case GETTING_BT_DEVICE:
			if(resultCode == RESULT_OK)
			{
				String sAddressDevice = data.getStringExtra(DeviceList_Activity.EXTRA_DEVICE_ADDRESS);
				TextView textBTSelectedDevice = (TextView)findViewById(R.id.textBTSelectedDevice);
				textBTSelectedDevice.setText(sAddressDevice);
				
				SharedPreferences settings = getSharedPreferences(SpidyCore_Activity.PREFS_NAME, 0);
				SharedPreferences.Editor settings_editor = settings.edit(); 
				settings_editor.putString(SpidyCore_Activity.PREFS_NAME_BTADDRESS, sAddressDevice);
				settings_editor.commit();
			}
			break;
		}
	}
}
